<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-编辑${name_china}</title>
<%@include file="/common/head.jsp" %>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>编辑${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="form" method="post">
    <div class="form-wrapper">
	  <div class="h4 distance-double">
        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
        <span class="h4-border"></span>
      </div>
        <input type="hidden" id="id" name="id"> 
        <!--  <div class="form-cont" id="hiddenvideo">
            <p class="form-cont-title"><span class="field-required">*</span>视频</p>
            这里写表数据的附件字段  fileId
            <input id="fileId" name="fileId" type="text" class="summer-input distance-right">
            <input id="videoId" name="videoId" type="text" class="summer-input distance-right">
            这里一定要写一个id隐藏
            <input type="hidden" id="id" name="id"> 
         </div>
      -->
      
      <div class="form-cont">
	        <p class="form-cont-title">视频</p>
	        <input type="hidden" id="vedioUrl" name="vedioUrl"/>
	   </div>
	     
         
	     <div class="form-cont">
	        <p class="form-cont-title">排序号</p>
	        <input type="text" id="reorder" name="reorder" class="summer-input summer-validatebox"  data-validatebox="validType:['noNegativeNumber']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>视频名称</p>
	        <input type="text" id="fileName" name="fileName" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">类别</p>
	        <input type="text" id="category" name="category" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[10]']"/> 
	     </div>
	      <input type="hidden" id="bonusPoints" name="bonusPoints" class="summer-input summer-validatebox"  data-validatebox="validType:['noNegativeNumber']"/> 
	     
	     
<!-- 	      <div class="form-cont">
	        <p class="form-cont-title">价格</p>
	        <input type="text" id="price" name="price" class="summer-input"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">作者</p>
	        <input type="text" id="author" name="author" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	     
	    <!--  <div class="form-cont">
	        <p class="form-cont-title"> 开始时间</p>
	        <input type="text" id="startTime" name="startTime" class=" summer-input summer-validatebox" onFocus="var afterTime=$dp.$('stopTime');WdatePicker({dateFmt:'yyyy-MM-dd',onpicked:function(){afterTime.focus();},minDate:'#F{(\'%y-%M-%d\')}',maxDate:'#F{$dp.$D(\'stopTime\')}'})" /> 
	     </div>
	     
	    <div class="form-cont">
	        <p class="form-cont-title">结束时间</p>
	        <input type="text" id="stopTime" name="stopTime" class="summer-input" onFocus="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'startTime\')}'})"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">详细时间格式</p>
	        <input type="text" id="dateDetail" name="dateDetail" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	 
<!-- 	    <div class="form-cont">
	        <p class="form-cont-title">出版日期</p>
	        <input type="text" id="publicationDate" name="publicationDate" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['maxLength[50]']"/> 
	     </div> -->
	     
	    
	     <div class="form-cont">
	        <p class="form-cont-title">视频说明</p>
	        <textarea class="ckeditor" id="styleText" name="styleText"></textarea>
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">备注</p>
	        <textarea class="summer-textarea" id="note" name="note"></textarea>
	     </div>
     
     <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
</form>
<script>
    
  var   editData,
        permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	editData = ${editData};
    	//alert(editData.id)
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:edit']
        }));
   	 
     	 //下拉初始化
   	    $('#category').combobox({
   	    	async:false,
   	        defaultValue: editData.category ,
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_WEB_VEDIO_CATEGORY'
        }); 
    	
    	//createConfig();// 初始化插件
	   	
        $('#form').form(/* {
        	onLoadSuccess:function(data){
        		var fileInfos = null;
        		if(editData.fileId != "" && editData.fileId != null){
                    fileInfos = getFileInfos(editData.fileId,"FILE_ID");// 获得文件信息
                    //alert(JSON.stringify(fileInfos));
                    //alert(JSON.stringify(fileInfos.FILE_ID));
                  if(fileInfos.FILE_ID !="" && fileInfos.FILE_ID != null){
                     createFileViewList(fileInfos.FILE_ID,"form");// 初始化文件列表
                  }
        		}
           }
        } */).form('loadDataEdit', {
            data: editData
        }); 
    	
          $("#vedioUrl").filePlugin({
    		   type:"video"
    	   });
    	
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:edit"]', submit);
    	
    	//返回按钮
    	$('#backBtn, #goBack').on('click',backMain);
    });
    
    
    

    
    //上传初始化方法
    function createConfig(){
    	initMyUploadConfig("fileId","videoId","hiddenvideo","T_WEB_VEDIO","${pageContext.session.id}");
    }
    
    //上传初始化方法
    function initMyUploadConfig(fileId,videoId,hiddenvideo,tableName,sessionId){
        //隐藏图片变量 添加图片回显默认页面
        $("#"+fileId).attr({"type" : "hidden"});
        //隐藏视频变量
        $("#"+videoId).attr({"type" : "hidden"});
        var vediostr = `<img id="VideoViewId" src="" width="80px" onerror="javascript:this.src='` + CONST_URL.BASEIMAGE +`'"/>`;
        vediostr = vediostr + `<input name="video" type="file" accept="video/mp4" id="video">`;
        $("#"+hiddenvideo).append(vediostr);
        initMutiFile(fileId,videoId,hiddenvideo,tableName,sessionId);
    }
    
        //初始化图片和视频添加事件(记得动态添加表名)
        function initMutiFile(fileId,videoId,hiddenvideo,tableName,sessionId){
  	
          //初始化视频上传事件
          var filtwo=$("#"+hiddenvideo); 
          filtwo.bind('change',function(){  

                      //1 上传视频到文件服务器  返回视频参数  =   视频id  视频地址  视频时长  视频第一帧地址 视频类型 视频大小  视频的属于哪个表
                      //id  fileUrl viewUrl itemType fileType fileSize ctime status dtime
              
                        var formdata = new FormData(); 
                        formdata.append("file", $("#video").get(0).files[0]);
                        formdata.append("ind", 2);
                        formdata.append("userId", sessionId);
                        formdata.append("fileType", "vedio");
                        formdata.append("tableName", tableName);

                        $.ajax({
                            type: "POST",
                            //url: "http://192.168.199.124:8080/FileServer/upload",
                            url: COMMON_URL.upload,
                            dataType: "json",//返回值
                            data: formdata,
                            sync:true,
                            cache : false, 
                            contentType : "multipart/form-data-urlencoded; charset=utf-8", 
                            contentType:false, //必须false才会避开jQuery对 formdata 的默认处理 , XMLHttpRequest会对 formdata 进行正确的处理
                            processData: false, //必须false才会自动加上正确的Content-Type 
                            success: function (ro) {
                            	//var jsonString = JSON.stringify(ro);
                                if (ro.code == 200) {
                                    //图片上传成功回调
                                    //alert(ro.datas.id);
                                    var ro_videoId = uploadFile(ro.datas,"vedioId");
                                    $("#videoId").attr({'value': ro_videoId});
                                    //归集到一个ID中
                                    $("#fileId").attr({'value':ro_videoId});
                                    $("#VideoViewId").attr({'src':ro.datas.url});
                                   // alert("视频数据到位:"+$("#videoId").val());
                                   // alert("文件ID(图片+视频)数据到位:"+$("#fileId").val());
                                }
                                else {//图片未上传成功回调
                                	alert(ro.message);
                                }
                            },
                            error: function (err) {
                                //服务器连接失败报错处理
                                alert("上传异常");
                                //alert(err.responseText);
                            },
                            beforeSend: function () {
                                //图片上传之前执行的操作，当前为进度条显示
                                //alert("图片正在上传中...");
                            }
                        });
              
              
          });
     }
        
        //上传图片和视频数据 + 文件字段
        function uploadFile(data,fieldv){
         	var ro_fileId = null;
            //保存数据到数据库   返回id
            $.sajax({
        		type : "get",
        		async : false,
        		//url : basePath + '/fileInfo/add',
        		url : COMMON_URL.fileinfoadd,
        		onLoadSuccess : function(ro){
        			if (ro.result == "success"){
        				//alert(ro.data);
        				ro_fileId = ro.data;
          			}
        		},
        		data:{
        			"tableName" : data.tableName,
        			"businessId" : data.id,
        			"field" : fieldv,
        			"fileName" : data.fileName,
        			"fileSuffix" : data.fileSuffix,
        			"fileSize" : data.fileSize,
        			"filePath" : data.url,
          			"vedioPath" : data.vedioUrl,
          			"fileTime" : data.fileLongTime
        		}
        	});
            
        	return ro_fileId;
        }
      
        //   用 fileIds 获取文件列表 field为列表返名
        function getFileInfos(fileIds,field){
        	 var fileInfo = null;
        	 //alert(basePath + '/fileInfo/getFileInfos');
        	 //alert(COMMON_URL.getFileInfos);
        	 //alert(fileIds);
        	 //return;
            $.sajax({
         		type : "get",
         		async : false,
         		//url : basePath + '/fileInfo/getFileInfos',
         		url : COMMON_URL.getFileInfos,
         		onLoadSuccess : function(ro){
         			if (ro.result == "success"){
         				//alert(ro.data);
         				fileInfo = ro.data;
         			}
         		},
         		data:{
         			"fileIds" : fileIds,
         			"field" : field,
         		}
         	});
        	 
            return fileInfo;
        }
        
        function createFileViewList(fileInfos,formId){
        	//fileId vedioId
        	$.each(fileInfos,function (index,fileInfo){
        		//alert(fileInfo.path);
        		//alert(fileInfo.type);
        		if(fileInfo.type == "vedioId"){
        		     $("#VideoViewId").attr("src",fileInfo.path);
        		     $("#videoId").attr("value",fileInfo.id);
        		     //alert("视频ID:"+$("#videoId").val())
        		}
        	});
        	var ro_fileId = $("#vedioId").val();
        	$("#fileId").attr("value",ro_fileId);
        }

        //表单提交
        function submit() {
             $("#form").form({
            	 buttons : ['#saveBtn'],
                 ajax: {
                     url: CONST_URL.EDIT
                 },
                 onSubmitSuccess: function (data) {
                	 $.stip({
       	                type:'ok',//显示类型 有info、 ok、 danger
       	                text: '保存成功！',
       	                onBeforeHide: function () {
       	                	backMain();
       	                }
                	 });
                  }
             }).form("submit");
        } 

        	 
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	EDIT: basePath + "${common_url}edit",
       	BASEIMAGE: basePath + "/static/images/addimage.png",
    };
    
</script>
</body>
</html>